//
//  AFRequestJSON.m
//  GraduationProject;
//
//  Created by Sylar on 16/4/13.
//  Copyright © 2016年 伍宏伟. All rights reserved.
//

#import "AFRequestJSON.h"
#import "AFNetworking.h"


#define SeneryURL @"http://apis.baidu.com/heweather/pro/attractions?cityid="
#define CityURL @"http://apis.baidu.com/heweather/pro/weather?city="

#define APIKey @"114d66be22bc4545b01f614bcd66ffff"

#define DefaultSeneryKey @"CN10101010018A"

#define DefaultCityId @"beijing"

@implementation AFRequestJSON


-(void)RequestCityByImportCityId:(NSString *)Id
{
    NSString *urlString;
    if (Id == nil)
    {
     urlString  = [NSString stringWithFormat:@"%@%@",CityURL,DefaultCityId];
    }
    else
    {
      urlString = [NSString stringWithFormat:@"%@%@",CityURL,Id];
    }
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager.requestSerializer setValue:APIKey forHTTPHeaderField:@"apikey"];
    
    [manager GET:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //成功回调城市JSON
        if ([self.delegate respondsToSelector:@selector(CityJSONSuccessReturn:)]) {
            
            [self.delegate CityJSONSuccessReturn:responseObject];
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //城市失败回调
        if ([self.delegate respondsToSelector:@selector(CityJSONFailReturn:)]) {
            
            [self.delegate CityJSONFailReturn:error];
            
        }
        
    }];

    
}



-(void)RequestSeneryByImportSceneryId:(NSString *)Id
{
    
    NSString *urlString;
    if (Id == nil)
    {
        urlString  = [NSString stringWithFormat:@"%@%@",SeneryURL,DefaultSeneryKey];
    }
    else
    {
        urlString = [NSString stringWithFormat:@"%@%@",SeneryURL,Id];
    }
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [manager.requestSerializer setValue:APIKey forHTTPHeaderField:@"apikey"];
    
    [manager GET:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //成功回调景区JSON
        if ([self.delegate respondsToSelector:@selector(SeneryJSONSuccessReturn:)]) {
            
            [self.delegate SeneryJSONSuccessReturn:responseObject];
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //景区失败回调
        if ([self.delegate respondsToSelector:@selector(SeneryJSONFailReturn:)]) {
            
            [self.delegate SeneryJSONFailReturn:error];
            
        }
        
    }];

    
}

//便利构造器

-(id)initWithDelegate:(id)delegate
{
    if (self = [super init])
    {
        
        _delegate = delegate;
        
    }
    return self;
}

+(id)Delegate:(id)Delegate RequestSeneryWithId:(NSString *)id
{
    AFRequestJSON *temp = [[self alloc]initWithDelegate:Delegate];
    
    [temp RequestSeneryByImportSceneryId:id];
    
    return temp;
}

+(id)Delegate:(id)Delegate RequestCityWithId:(NSString *)id
{
    AFRequestJSON *temp = [[self alloc]initWithDelegate:Delegate];
    
    [temp RequestCityByImportCityId:id];
    
    return temp;
}

@end
